/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package java_core;

import java.util.Arrays;


/**
 *
 * @author Yana
 */
public class Merger {

    public static int[] merge(int[] a, int[] b) { // wrong!
        int[] result = new int[a.length + b.length];
        int aIndex = 0;
        int bIndex = 0;
        while (aIndex + bIndex != result.length) {
            if (a[aIndex] <= b[bIndex]) {
                result[aIndex + bIndex] = a[aIndex++];
            } else {
                result[aIndex + bIndex] = b[bIndex++];
            }
        }
        return result;
    }
}

class TestMerge {
    public static void main(String[] args) {
        int[][][] data = {
                {{}, {}},
                {{0}, {}},
                {{}, {0}},
                {{0}, {0}},
                {{0, 2}, {1, 3}},
                {{0, 2, 7, 9, 123}, {1, 3, 4, 5, 6}},
        };
        for (int[][] origin : data) {
            int[] left = origin[0];
            int[] right = origin[1];
            int[] merged = Merger.merge(left, right);
            System.out.println(
                    Arrays.toString(left) +
                    " + " +
                    Arrays.toString(right) +
                    " -> " +
                    Arrays.toString(merged));
            System.out.println();
        }
    }
}
